<?php

/**
 * timecards actions.
 *
 * @package    virtualstore
 * @subpackage timecards
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class timecardsActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->time_cards = Doctrine::getTable('TimeCard')
                        ->createQuery('a')
                        ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $tcid = $request->getParameter('id');
        $this->time_card = Doctrine::getTable('TimeCard')->find(array($tcid));
        $this->timeCards = Doctrine::getTable('TimeCardDay')
                        ->findByTimeCardId($tcid);
        $this->forward404Unless($this->time_card);
    }

    public function executeShowTimeCards(sfWebRequest $request) {
        $this->time_card = Doctrine::getTable('TimeCard')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->time_card);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new TimeCardForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new TimeCardForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($time_card = Doctrine::getTable('TimeCard')->find(array($request->getParameter('id'))), sprintf('Object time_card does not exist (%s).', $request->getParameter('id')));
        $this->form = new TimeCardForm($time_card);

        $tcid = $request->getParameter('id');
        //$employee = new Employee();
        $employee = $time_card->getEmployee();

        $timeCards = Doctrine::getTable('TimeCardDay')
                        ->findByTimeCardId($tcid);
        $this->timeCardDayForms = array();
        $this->availableShifts = array();
        $isi=0;
        //$positions = $employee->getPositions();


        $i=0;
        foreach ($timeCards as $tc) {
          $cshifts = $tc->getPosition()->getShiftAssignment();//->getShift();
//          $this->logMessage($tc->getDate() . ' Shift: ' . count($cshifts),'debug');
          foreach ($cshifts as $cshift) {
              if(!array_search($cshift, $this->availableShifts)){
                  $this->availableShifts[$cshift->getShift()->getId()] = $cshift->getShift();
                  $isi++;
              }
          }
          $this->timeCardDayForms[$i] = new TimeCardDayForm($tc);
          $i++;
        }
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($time_card = Doctrine::getTable('TimeCard')->find(array($request->getParameter('id'))), sprintf('Object time_card does not exist (%s).', $request->getParameter('id')));
        $this->form = new TimeCardForm($time_card);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($time_card = Doctrine::getTable('TimeCard')->find(array($request->getParameter('id'))), sprintf('Object time_card does not exist (%s).', $request->getParameter('id')));
        $time_card->delete();

        $this->redirect('timecards/index');
    }
    public function executeGetHoursForShift(sfWebRequest $request){
        $cdate = $request->getParameter('cdate');
        $position_id = $request->getParameter('position_id');
        $shift_id = $request->getParameter('shift_id');

//        $dow = date('N', strtotime($cdate));
        //$this->logMessage("$cdate : dow $dow", 'debug');
        //echo "$cdate : dow $dow";
        $cshift = Doctrine::getTable('ShiftDay')->findShiftForDay($shift_id, $cdate);
        $template ='{"shiftDayId" : "%s", "hours" : ["%s", "%s","%s" ], "restday" : "%s"}';
     
        $hours = sprintf($template, $cshift->getId(),
                $cshift->getShift1(), $cshift->getShift2(),
                $cshift->getShift3(), $cshift->getRestDay());
        return $this->renderText($hours);
        //return $this->renderText('07:00-11:30');

    }
    public function executeBatchUpdateTimeCards(sfWebRequest $request){
        $this->shifts = $request->getParameter('shift1');

        $shifts1 = $request->getParameter('shift1');
        $shifts2 = $request->getParameter('shift2');
        $shifts3 = $request->getParameter('shift3');
        $restDays = $request->getParameter('restDay');
        $shift_day_ids = $request->getParameter('shift_day_id');
        $tcdt = Doctrine::getTable('TimeCardDay');
        $shiftDayTable = Doctrine::getTable('ShiftDay');
        foreach ($shifts1 as $key => $shift1) {
            $tcd = $tcdt->find($key);
            $tcd->setShift1($shift1);
            $tcd->setShift2($shifts2[$key]);
            $tcd->setShift3($shifts3[$key]);
            $tcd->setRestDay($restDays[$key]);
            $this->logMessage('ShiftDayId ' . $key . ' = ' . $shift_day_ids[$key], 'debug');
            $shiftDay = $shiftDayTable->find($shift_day_ids[$key]);
            if($shiftDay != null){
                $tcd->setShiftDay($shiftDay);
            }
            $tcd->save();

           // echo "Key $key = $value\n";
        }
//        $this->redirect('timecards/index');
    }
    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $time_card = $form->save();

            $this->redirect('timecards/edit?id=' . $time_card->getId());
        }
    }

}
